// 用于导入uniWrapper存储于浏览器中的信息

import uniWrapper from "../store/uniWrapper";
import {mapActions} from "vuex";
import language from "../../api/language";

export default {
    // mounted() {
    //     this.loadLanguageCache()
    //     this.loadUserInfoCache()
    // },
    methods: {
        ...mapActions({
            setNowLanguage: 'app/setNowLanguage',
            setUserInfo: 'user/setUserInfo',
            setUserHasLogin: 'user/setUserHasLogin'
        }),
        loadAllCache(){
            this.loadLanguageCache()
            this.loadUserInfoCache()
        },
        getModalTranslate(posIndex){
            let langDict = this.language.getTranslate(language.SystemModal)
            if (posIndex === 0){
                return langDict.titleTip
            }else if(posIndex === 1){
                return langDict.titleError
            }else if(posIndex === 2){
                return langDict.redirectError
            }
        },
        loadLanguageCache(){
            let lang = uniWrapper.getUniCache('nowLanguage')
            if(lang){
                this.setNowLanguage(lang)
            }
        },
        loadUserInfoCache(){
            let userInfo = uniWrapper.getUniCache('userInfo')
            let that = this
            if(userInfo){
                this.setUserInfo(userInfo)
                this.setUserHasLogin(true)
            }else{
                this.setUserHasLogin(false)
                uni.redirectTo({
                    url: '/pages/login/login',
                    fail: () => {
                        uni.showModal({
                            title: 'Error',
                            content: that.getModalTranslate(2)
                        })
                    }
                })
            }
        }
    }
}
